package caffeine;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.time.Duration;
import java.util.ArrayList;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-10-25 9:35
 */
public class Test3 {

    public static void main(String[] args) throws InterruptedException {
        Cache<String, String> cache = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(2)).build();

        cache.put("123","456");
        cache.put("a","b");

        System.out.println( cache.getIfPresent("123"));
        System.out.println( cache.getIfPresent("a"));
        System.out.println( cache.getIfPresent("b"));

        Thread.sleep(3000L);

        String s = cache.get("123", key -> "bbb");
        System.out.println( s);

        System.out.println( cache.getIfPresent("123"));

        //清空缓存中的数据
        cache.invalidateAll();
        cache.invalidate("123");

        System.out.println( cache.getIfPresent("123"));
    }
}
